package edu.edu_realtime_publisher_second.controller;

import edu.edu_realtime_publisher_second.mapper.TestCourseTestPaper;
import edu.edu_realtime_publisher_second.mapper.TestScoreRangeBean;
import edu.edu_realtime_publisher_second.mapper.TradeProvinceAmount;
import edu.edu_realtime_publisher_second.mapper.TradeSourceTradeSum;
import edu.edu_realtime_publisher_second.service.SugarSevice;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@RestController
public class SugarController {

    @Autowired
    private SugarSevice sugarSevice;

    @RequestMapping("/provinceAmount")
    public String getTradeProvinceAmount(@RequestParam(value = "date",defaultValue = "0") int date){
        if (date == 0){
            date =getToday();
        }
        List<TradeProvinceAmount> tradeProvinceAmountList = sugarSevice.getTradeProvinceAmount(date);
        if (tradeProvinceAmountList == null){
            return "";
        }

        StringBuilder mapData = new StringBuilder("[");
        for (int i = 0; i < tradeProvinceAmountList.size(); i++) {
            TradeProvinceAmount tradeProvinceAmount = tradeProvinceAmountList.get(i);
            String provinceName = tradeProvinceAmount.getProvinceName();
            Double finalAmount = tradeProvinceAmount.getFinalAmount();

            mapData.append("{\n"+
                    "       \"name\": \""+provinceName+"\",\n" +
                    "       \"value\": "+finalAmount+"\n"+
                    "        }"
            );
            if (i<tradeProvinceAmountList.size()-1){
                mapData.append(",");
            }else{
                mapData.append("]");
            }
        }
        return "{\n" +
                "  \"status\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": {\n" +
                "    \"mapData\":"+mapData+",\n" +
                "    \"valueName\":\"下单金额\"\n" +
                "  }\n" +
                "}";
    }


    //@RequestMapping("/tradeSourceSum")
    public String getTradeSourceSum(@RequestParam(value = "date",defaultValue = "0") int date){
        if (date ==0){
            date = getToday();
        }
        List<TradeSourceTradeSum> tradeSourceSumList = sugarSevice.getTradeSourceSum(date);
        if (tradeSourceSumList == null){
            return "";
        }

        StringBuilder seriesBuilder = new StringBuilder("[");
        for (int i = 0; i < tradeSourceSumList.size(); i++) {
            TradeSourceTradeSum bean = tradeSourceSumList.get(i);
            String sourceName = bean.getSourceName();

            ArrayList<Double> data = new ArrayList<>();
            data.add(bean.getFinalAmount());
            data.add(bean.getPersonCount());
            data.add(bean.getOrderCount());

            seriesBuilder.append(
                    "{\n" +
                    "    \"name\":\""+sourceName+"\",\n" +
                    "    \"data\":["+StringUtils.join(data,",")+"]\n" +
                    "}"
            );
            if (i<tradeSourceSumList.size()-1){
                seriesBuilder.append(",");
            }else{
                seriesBuilder.append("]");
            }
        }
        return "{\n" +
                "  \"status\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": {\n" +
                "    \"categories\": [\n" +
                "      \"finalAmount\",\n" +
                "      \"personCount\",\n" +
                "      \"orderCount\"\n" +
                "    ],\n" +
                "    \"series\": "+seriesBuilder+"\n" +
                "   }\n"+
                "}";
    }

    @RequestMapping("/courseTest")
    public String getTestCourseTestPaper(@RequestParam(value = "date",defaultValue = "0") int date,
                                         @RequestParam(value = "limit",defaultValue = "5") int limit){
        if (date == 0){
            date = getToday();
        }

        List<TestCourseTestPaper> testCourseTestPaperList = sugarSevice.getTestCourseTestPaper(date,limit);
        if (testCourseTestPaperList == null){
            return "";
        }
        StringBuilder rowsBuilder = new StringBuilder("[");
        for (int i = 0; i < testCourseTestPaperList.size(); i++) {
            TestCourseTestPaper bean = testCourseTestPaperList.get(i);

            String courseName = bean.getCourseName();
            Integer personCt = bean.getPersonCt();

            String scoreAvg = bean.getScoreAvg();
            int i1 = bean.getScoreAvg().indexOf(".");
            if (i1 >=0){
                scoreAvg = scoreAvg.substring(0, i1+3);
            }

            String durationAvg = bean.getDurationAvg();
            int i2 = bean.getDurationAvg().indexOf(".");
            if (i2>=0){
                durationAvg = durationAvg.substring(0,i2+3);
            }

            //System.out.println("person>>>>>>>>>>>>");
            rowsBuilder.append(
                    "{\n" +
                            "    \"c1\":\""+courseName+"\",\n" +
                            "    \"c2\":"+personCt+",\n" +
                            "    \"c3\":\""+scoreAvg+"\",\n" +
                            "    \"c4\":\""+durationAvg+"\"\n" +
                            "}"
            );
            if (i<testCourseTestPaperList.size()-1){
                rowsBuilder.append(",");
            }else{
                rowsBuilder.append("]");
            }
        }

        return  "{\n" +
                "  \"status\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": {\n" +
                "    \"combineNum\": 1,\n" +
                "    \"columns\": [\n" +
                "      {\n" +
                "        \"name\": \"courseName\",\n" +
                "        \"id\": \"c1\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"personCt\",\n" +
                "        \"id\": \"c2\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"scoreAvg\",\n" +
                "        \"id\": \"c3\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"durationAvg\",\n" +
                "        \"id\": \"c4\"\n" +
                "      }\n" +
                "    ],\n" +
                "    \"rows\":"+rowsBuilder+
                "  }\n" +
                "}";
    }

    @RequestMapping("/scoreRang")
    public String getTestScoreRange(@RequestParam(value = "date",defaultValue = "0") int date,
                                    @RequestParam(value = "limit",defaultValue = "5") int limit){
        if (date == 0){
            date = getToday();
        }

        List<TestScoreRangeBean> testScoreRangeList = sugarSevice.getTestScoreRange(date, limit);
        if (testScoreRangeList == null){
            return "";
        }

        ArrayList<String> cateList = new ArrayList<>();
        ArrayList<Integer> personList = new ArrayList<>();
        ArrayList<Integer> paperList = new ArrayList<>();

        for (int i = 0; i < testScoreRangeList.size(); i++) {
            TestScoreRangeBean bean = testScoreRangeList.get(i);

            String nameAndRange = bean.getPaperName() +"-"+ bean.getRange();
            cateList.add(nameAndRange);
            personList.add(bean.getPersonCt());
            paperList.add(bean.getPaperCt());
        }
        return "{\n" +
                "  \"status\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": {\n" +
                "    \"series\": [\n" +
                "      {\n" +
                "        \"name\": \"报考人数\",\n" +
                "        \"data\": [\n" +
                StringUtils.join(personList,",") +
                "        ],\n" +
                "        \"type\": \"bar\",\n" +
                "        \"yAxisIndex\": 0\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"实考人数\",\n" +
                "        \"data\": [\n" +
                StringUtils.join(paperList,",") +
                "        ],\n" +
                "        \"type\": \"line\",\n" +
                "        \"yAxisIndex\": 1\n" +
                "      }\n" +
                "    ],\n" +
                "    \"categories\": [\n" +
                "\""+StringUtils.join(cateList,"\",\"")+
                "\"    ]\n" +
                "  }\n" +
                "}";

    }



    private int getToday() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        return Integer.parseInt(sdf.format(System.currentTimeMillis()));
    }
}
